package com.mouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mouse.entity.*;
import com.mouse.entity.vo.DoubleAlleleVO;
import com.mouse.mapper.*;
import com.mouse.service.DoubleAllelesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mouse.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author godson
 * @since 2022-08-07
 */
@Service
public class DoubleAllelesServiceImpl extends ServiceImpl<DoubleAllelesMapper, DoubleAlleles> implements DoubleAllelesService {
    @Autowired
    GeneCategoryMapper geneCategoryMapper;

    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private DoubleAllelesMapper doubleAllelesMapper;

    @Autowired
    private PhenotypeMapper phenotypeMapper;

    @Autowired
    private ReferenceMapper referenceMapper;

    @Autowired
    private GeneCompositionDoubleAllelesMapper geneCompositionDoubleAllelesMapper;

    @Autowired
    private MouseMapper mouseMapper;

    @Autowired
    private GeneReferenceMapper geneReferenceMapper;

    @Autowired
    private GeneCompositionReferenceMapper geneCompositionReferenceMapper;

    @Autowired
    private GenecategoryGenecompositionMapper genecategoryGenecompositionMapper;

    @Autowired
    private GeneAlleleMapper geneAlleleMapper;
    @Autowired
    private GeneHomologyMapper geneHomologyMapper;

    @Autowired
    private DoubleAllelesGeneAlleleMapper doubleAllelesGeneAlleleMapper;

    @Autowired
    private GeneCompositionMapper geneCompositionMapper;

    /***
     * 查询：根据一个字段信息查询表格信息，支持字段值类型为ID和String
     * @param page 分页设置
     * @param key 字段名
     * @param value 字段值
     * @return 查询结果
     */
    @Override
    public IPage<DoubleAlleles> getInfoByMap(IPage<DoubleAlleles> page, String key, String value){
        long id;
        QueryWrapper<DoubleAlleles> wrapper = new QueryWrapper<>();
        if(StringUtil.isInteger(value)){
            id = Long.parseLong(value);
            wrapper.eq(key,id);
        } else {
            wrapper.eq(key,value);
        }
        return baseMapper.selectPage(page,wrapper);
    }
    
    @Override
    public DoubleAlleleVO getDoubleAlleleVO(DoubleAlleles doubleAlleles){


        /**
         * key:da.did
         */
         Map<Long, List<GeneComposition>> geneCompositionMap=new HashMap();

        /**
         * key:gcom.gcid
         */
         Map<Long, List<Reference>> geneCompositionReferenceMap=new HashMap();


        /**
         /**
         * key:gcom.gcid
         */
         Map<Long, List<Phenotype>> phenotypeMap=new HashMap();

        /**
         * key:gcom.gcid
         */
         Map<Long, List<GeneCategory>> geneCategoryMap=new HashMap();

        /**
         * key:gc.gid
         */
         Map<Long,List<Disease>> diseaseMap=new HashMap();

        /**
         * key:da.did
         */
         Map<Long, List<GeneAllele>> geneAlleleMap=new HashMap();

         Long id = doubleAlleles.getDid();
        //GeneComposition相关
        List<GeneCompositionDoubleAlleles> geneCompositionDoubleAllelesByDid = geneCompositionDoubleAllelesMapper.getGeneCompositionDoubleAllelesByDid(id);
        List<GeneComposition> geneCompositionByDid = geneCompositionMapper.getGeneCompositionByDid(id);
        for(GeneCompositionDoubleAlleles relation: geneCompositionDoubleAllelesByDid){
            geneCompositionMap.computeIfAbsent(relation.getDid(), k -> new ArrayList<>());
            for (GeneComposition entity:geneCompositionByDid){
                if(entity.getGcid().equals(relation.getGcid())) {
                    geneCompositionMap.get(relation.getDid()).add(entity);
                }
            }
        }

        List<Mouse> mouseByDid = mouseMapper.getMouseByDid(id);
        List<Phenotype> phenotypeByDid = phenotypeMapper.getPhenotypeByDid(id);
        for(Mouse relation: mouseByDid){
            phenotypeMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Phenotype entity:phenotypeByDid){
                if(entity.getPid().equals(relation.getPid())) {
                    phenotypeMap.get(relation.getGcid()).add(entity);
                }
            }
        }
        List<GeneCompositionReference> geneCompositionReferenceByDid = geneCompositionReferenceMapper.getGeneCompositionReferenceByDid(id);
        List<Reference> referenceByDid = referenceMapper.getReferenceByDid(id);
        for(GeneCompositionReference relation: geneCompositionReferenceByDid){
            geneCompositionReferenceMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (Reference entity:referenceByDid){
                if(entity.getRid().equals(relation.getRid())) {
                    geneCompositionReferenceMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        //GeneCategory相关
        List<GenecategoryGenecomposition> genecategoryGenecompositionByDid = genecategoryGenecompositionMapper.getGenecategoryGenecompositionByDid(id);
        List<GeneCategory> geneCategoryByDid = geneCategoryMapper.getGeneCategoryByDid(id);
        for(GenecategoryGenecomposition relation: genecategoryGenecompositionByDid){
            geneCategoryMap.computeIfAbsent(relation.getGcid(), k -> new ArrayList<>());
            for (GeneCategory entity:geneCategoryByDid){
                if(entity.getGid().equals(relation.getGid())) {
                    geneCategoryMap.get(relation.getGcid()).add(entity);
                }
            }
        }

        List<GeneHomology> geneHomologyByDid = geneHomologyMapper.getGeneHomologyByDid(id);
        List<Disease> diseaseByDid = diseaseMapper.getDiseaseByDid(id);
        for(GeneHomology relation: geneHomologyByDid){
            diseaseMap.computeIfAbsent(relation.getGid(), k -> new ArrayList<>());
            for (Disease entity:diseaseByDid){
                if(entity.getSid().equals(relation.getSid())) {
                    diseaseMap.get(relation.getGid()).add(entity);
                }
            }
        }

        List<DoubleAllelesGeneAllele> doubleAllelesGeneAlleleByDid = doubleAllelesGeneAlleleMapper.getDoubleAllelesGeneAlleleByDid(id);
        List<GeneAllele> geneAllelesByDid = geneAlleleMapper.getGeneAllelesByDid(id);
        for(DoubleAllelesGeneAllele relation: doubleAllelesGeneAlleleByDid){
            geneAlleleMap.computeIfAbsent(relation.getDid(), k -> new ArrayList<>());
            for (GeneAllele entity:geneAllelesByDid){
                if(entity.getAid().equals(relation.getAid())) {
                    geneAlleleMap.get(relation.getDid()).add(entity);
                }
            }
        }

        DoubleAlleleVO doubleAlleleVO = new DoubleAlleleVO();

        doubleAlleleVO.setDoubleAlleles(doubleAlleles);
        doubleAlleleVO.setGeneCompositionMap(geneCompositionMap);
        doubleAlleleVO.setPhenotypeMap(phenotypeMap);
        doubleAlleleVO.setGeneCompositionReferenceMap(geneCompositionReferenceMap);
        doubleAlleleVO.setGeneCategoryMap(geneCategoryMap);
        doubleAlleleVO.setDiseaseMap(diseaseMap);
        doubleAlleleVO.setGeneAlleleMap(geneAlleleMap);

        return doubleAlleleVO;


    }
//    @Autowired
//    DoubleAllelesMapper doubleAllelesMapper;
//
//    public DoubleAlleles getDoubleAllelesByName(Long geneId) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("gid", geneId);
//        return doubleAllelesMapper.selectPage(null,new QueryWrapper<>().)
//    }
}
